package drytools.site.css;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.w3c.css.sac.InputSource;
import org.w3c.dom.css.CSSRule;
import org.w3c.dom.css.CSSRuleList;
import org.w3c.dom.css.CSSStyleDeclaration;
import org.w3c.dom.css.CSSStyleRule;
import org.w3c.dom.css.CSSStyleSheet;

import com.steadystate.css.parser.CSSOMParser;

public class EditableCSSList {
	List<CSSStyleRule> allKnownRules;
	Map<String, CSSStyleRule> rulesMappedBySelectors;
	Map<String, List<EditableCSSRule>> rulesMappedByName;
	List<EditableCSSRule> editableRules;
	
	public Map<String, List<EditableCSSRule>> getRulesMappedByName() {
		if (rulesMappedByName == null) {
			rulesMappedByName = new LinkedHashMap<String, List<EditableCSSRule>>();
		}
		return rulesMappedByName;
	}


	public void setRulesMappedBySelectors(
			Map<String, CSSStyleRule> rulesMappedBySelectors) {
		this.rulesMappedBySelectors = rulesMappedBySelectors;
	}

	public Map<String, CSSStyleRule> getRulesMappedBySelectors() {
		if (rulesMappedBySelectors == null) {
			rulesMappedBySelectors = new LinkedHashMap<String, CSSStyleRule>();

			for (CSSStyleRule cssRule : getAllKnownRules()) {
				rulesMappedBySelectors.put(cssRule.getSelectorText(), cssRule);
			}
		}
		return rulesMappedBySelectors;
	}

	public List<CSSStyleRule> getAllKnownRules() {
		if (allKnownRules == null) {
			// String fileName =
			// "C:/pureprocessWorkspace/TestICEFaces/WebContent/xmlhttp/css/rime/rime.css"
			// ;
			String fileName = "/xmlhttp/css/rime/rime.css";
			CSSOMParser parser = new CSSOMParser();

			// InputSource inputSource = new InputSource(new
			// FileReader(fileName));
			InputStream in = FacesContext.getCurrentInstance()
					.getExternalContext().getResourceAsStream(fileName);
			InputSource inputSource = new InputSource(new InputStreamReader(in));
			try {
				CSSStyleSheet style = parser.parseStyleSheet(inputSource, null,
						null);
				allKnownRules = new ArrayList<CSSStyleRule>();

				CSSRuleList rulesList = style.getCssRules();
				for (int i = 0; i < rulesList.getLength(); i++) {
					CSSRule rule = rulesList.item(i);
					//System.out.println("rule:" + rule);
					if (rule.getType() == rule.STYLE_RULE) {
						CSSStyleRule cssRule = (CSSStyleRule) rule;
						allKnownRules.add(cssRule);
					}

				}
			} catch (IOException e) {

				e.printStackTrace();
			}

		}
		return allKnownRules;
	}

	public void setAllKnownRules(List<CSSStyleRule> allKnownRules) {
		this.allKnownRules = allKnownRules;
	}

	public List<EditableCSSRule> getEditableRules() {
		if (editableRules == null) {
			editableRules = new ArrayList<EditableCSSRule>();
		}
		return editableRules;
	}

	public void setEditableRules(List<EditableCSSRule> editableRules) {
		this.editableRules = editableRules;
	}


	public void addRuleToMapByName(String styleName,
			EditableCSSRule editableRule) {
		List<EditableCSSRule> list = getRulesMappedByName().get(styleName);
		if (list == null) {
			list = new ArrayList<EditableCSSRule>();
			getRulesMappedByName().put(styleName, list);
		}
		list.add(editableRule);
		
	}

}
